$(function(){
	var dataTable = $("#bpPostDataTable").DataTable({
		"ajax": {
			"url": '/bpPost/list',
			"data": function(data){
//				data.nickName = $("#nickName").val();
				data.channelId = $("#channelId").val();
				data.isClassic = $("#isClassic").val();
				data.isTop = $("#isTop").val();
				data.releaseTime = $("#releaseTime").val();
				data.status = $("#status").val();
			}
		},
		"ordering": false,
		"columns": [
//			{
//				"data": "id",
//				"className": "text-center sign-col",
//				"render": function(data, type, row, meta){
//					return '<input type="checkbox" name="pk" value="' + data + '" onclick="checkOne()"/>';
//				}
//			},
//			{"data": "nickName"},
//			{"data": "images"},
			{
				"data": function(obj){
					var text = '';
					if(obj.images != null && obj.images != ''){
						var imgs = obj.images.split(",");
						for(var i=0;i<imgs.length;i++){
							text +='<img alt="" src="http://39.105.119.87:8088'+imgs[i]+'" width="40" height="40"/>&nbsp;';
						}
					} else {
						text = '-';
					}
					return text;
				}
			},
			{"data": "content"},
//			{"data": "channelId"},
//			{"data": "channelName"},
			{
				"data": function(obj){
					var text = '';
					if (obj.channelId == '7dd32f3e6ad945e4af9ee7fed61489d8') {
						text = '今日开心事';
					} else if (obj.channelId == '1c83e93597a54ea496e46c3a1040668f') {
						text = '今日烦恼事';
					} else if (obj.channelId == '2c9ccceb255444feb1a165f201ceee1b') {
						text = '防骗秘籍';
					} else if (obj.channelId == 'bf2f84c2c4ae44f79e41455157845368') {
						text = '五险一金问题';
					} else if (obj.channelId == 'f71713a441584b08a1745f2da4165ee3') {
						text = '发工资啦';
					} else if (obj.channelId == '1433229ab95c4814962ba728bb59cd8d') {
						text = '意见反馈';
					} else {
						text = '未知';
					}
					return text;
				}
			},
//			{"data": "isClassic"},
			{
				"data": function(obj){
					var text = '';
					if (obj.isClassic == '0') {
						text = '否';
					} else if (obj.isClassic == '1') {
						text = '是';
					}
					return text;
				}
			},
//			{"data": "isTop"},
			{
				"data": function(obj){
					var text = '';
					if (obj.isTop == '0') {
						text = '否';
					} else if (obj.isTop == '1') {
						text = '是';
					}
					return text;
				}
			},
			{
				"data": function(obj){
					return new Date(obj.releaseTime).Format("yyyy-MM-dd hh:mm:ss");
				} 
			},
//			{"data": "status"},
			{
				"data": function(obj){
					var text = '';
					if (obj.status == '0') {
						text = '屏蔽';
					} else if (obj.status == '1') {
						text = '正常';
					} else if (obj.status == '2') {
						text = '删除';
					}
					return text;
				}
			},
			{
				"data": "id",
				"className": "operate-col",
				"data": function(data){
//					var html = '<button type="button" onclick="edit(\''+ data +'\')" class="btn btn-sm btn-warning right-5"><i class="fa fa-edit"></i></button>'
					var html = '<button type="button" onclick="view(\''+ data.id +'\')" class="btn btn-sm btn-info"><i class="fa fa-search"></i></button>';
//					var html = '';
					if(data.isClassic == '0' && data.status == '1'){
						html += '<button type="button" onclick="changeIsClassic(\''+ data.id +'\', \'1\')" class="btn btn-sm btn-info">加精</button>';
					} else if(data.isClassic == '1' && data.status == '1') {
						html += '<button type="button" onclick="changeIsClassic(\''+ data.id +'\', \'0\')" class="btn btn-sm btn-info">取消加精</button>';
					}
					if(data.isTop == '0' && data.status == '1'){
						html += '<button type="button" onclick="changeIsTop(\''+ data.id +'\', \'1\')" class="btn btn-sm btn-info">置顶</button>';
					} else if(data.isTop == '1' && data.status == '1'){
						html += '<button type="button" onclick="changeIsTop(\''+ data.id +'\', \'0\')" class="btn btn-sm btn-info">取消置顶</button>';
					}
					if(data.status == '1'){
						html += '<button type="button" onclick="changeStatus(\''+ data.id +'\', \'0\')" class="btn btn-sm btn-info">屏蔽</button>';
					}
					if(data.status == '0'){
						html += '<button type="button" onclick="deleteData(\''+ data.id +'\')" class="btn btn-sm btn-info">删除</button>';
					}
					return html;
				}
			}
		]
	});
	
	$('#searchBtn').click(function(){
		dataTable.ajax.reload();
	});
	
	$('#resetBtn').click(function(){
		$('#bpPostSearchForm')[0].reset();
		dataTable.ajax.reload();
	});
	
	$('#addBtn').click(function(){
		forwardPage('/bpPost/bpPost');
	});
	
	$('#delBtn').click(function(){
		ajaxBySelecteRows(dataTable,'/bpPost/delete','确定要删除？');
	});
	
	// 日期
	$('.datepicker').datetimepicker({
		language: 'zh-CN',
		format: 'yyyy-mm-dd',
		autoclose: true,
		todayBtn: true,
		clearBtn:true,
		minView: 2
	});
});

//function edit(id){
//	forwardPage('/bpPost/bpPost?id='+id);
//}

function view(id){
	forwardPage('/bpPost/view?id='+id);
}
function changeStatus(id, status){
	$.ajax({
		type: 'post',
		url: '/bpPost/changeStatus',
		data: {id: id, status: status},
		dataType: 'json',
		success: function(response){
			modalAlert(response.message);
			$("#bpPostDataTable").DataTable().ajax.reload();
		}
	});
}
function changeIsClassic(id, isClassic){
	$.ajax({
		type: 'post',
		url: '/bpPost/changeIsClassic',
		data: {id: id, isClassic: isClassic},
		dataType: 'json',
		success: function(response){
			modalAlert(response.message);
			$("#bpPostDataTable").DataTable().ajax.reload();
		}
	});
}
function changeIsTop(id, isTop){
	$.ajax({
		type: 'post',
		url: '/bpPost/changeIsTop',
		data: {id: id, isTop: isTop},
		dataType: 'json',
		success: function(response){
			modalAlert(response.message);
			$("#bpPostDataTable").DataTable().ajax.reload();
		}
	});
}
function deleteData(id){
	$.ajax({
		type: 'post',
		url: '/bpPost/delete',
		data: {id: id},
		dataType: 'json',
		success: function(response){
			modalAlert(response.message);
			$("#bpPostDataTable").DataTable().ajax.reload();
		}
	});
}